package com.lonely.fx;

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @date: 2022-07-27 17:36
 * @author: ztkj-hzb
 * @description:
 **/
public class FireEventSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();
        hBox.setSpacing(30);
        hBox.setPadding(new Insets(50));

        Button button1 = new Button("Button1");
        Button button2 = new Button("Button2");


        button1.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("Button1 click");

                //TODO 手动调用事件，将指定事件传递给目标对象
                //这里，将点击事件传递给button2，这样会触发button2的点击事件
                //Event.fireEvent(button2,event);

                //事件复制，将指定事件复制给新的数据源
                MouseEvent mouseEvent = event.copyFor(button2, button2);
                Event.fireEvent(mouseEvent.getTarget(),mouseEvent);
            }
        });

        button2.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("Button2 click");
            }
        });



        hBox.getChildren().addAll(button1,button2);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();
    }
}
